<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>机蜂博客</title>
    <base href="<%=request.getContextPath()%>/">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="static/layui/css/layui.css">
    <script src="static/jquery/jquery.js"></script>
    <script src="static/jquery/js.cookie.js"></script>
    <script src="static/layui/layui.js"></script>
</head>
<body>
<div style="padding: 0 20px">
    <div class="layui-tab layui-tab-brief" lay-filter="loginTab">
        <ul class="layui-tab-title" style="text-align: center">
            <li lay-id="0">登录</li>
            <li lay-id="1">注册</li>
        </ul>
        <div class="layui-tab-content">
            <div class="layui-tab-item">
                <form class="layui-form">
                    <div class="demo-login-container">
                        <div class="layui-form-item">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-username"></i>
                                </div>
                                <input type="text" name="username" value="" lay-verify="required|uname"
                                       placeholder="用户名"
                                       lay-reqtext="请填写用户名" autocomplete="off" class="layui-input"
                                       lay-affix="clear">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-password"></i>
                                </div>
                                <input type="password" name="password" value="" lay-verify="required"
                                       placeholder="密   码" lay-reqtext="请填写密码" autocomplete="off"
                                       class="layui-input" lay-affix="eye">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-row">
                                <div class="layui-col-xs7">
                                    <div class="layui-input-wrap">
                                        <div class="layui-input-prefix">
                                            <i class="layui-icon layui-icon-vercode"></i>
                                        </div>
                                        <input type="text" name="code" value="" lay-verify="required"
                                               placeholder="验证码" lay-reqtext="请填写验证码" autocomplete="off"
                                               class="layui-input" lay-affix="clear">
                                    </div>
                                </div>
                                <div class="layui-col-xs5">
                                    <div style="margin-left: 10px;">
                                        <img style="display: block;height: 38px;width: 100%;" id="kaptchaImage"
                                             alt="加载异常"
                                             title="点击更换" onclick="getKaptcha()">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <input type="checkbox" value="1" name="remember" lay-skin="primary" title="7天免登录">
                            <a href="javascript:;" onclick="forget()"
                               style="float: right; margin-top: 7px;color: #01aaed">忘记密码？</a>
                        </div>
                        <div class="layui-form-item">
                            <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="loginBtn">登录</button>
                        </div>
                        <div class="layui-form-item" style="text-align: right;">
                            没有账号立即 <a href="javascript:;" onclick="changeTab(1)" style="color: #01aaed">注册</a>
                        </div>
                    </div>
                </form>
            </div>
            <div class="layui-tab-item">
                <form class="layui-form" id="registerForm">
                    <div class="demo-reg-container">
                        <div class="layui-form-item">
                            <div class="layui-row">
                                <div class="layui-col-xs7">
                                    <div class="layui-input-wrap">
                                        <div class="layui-input-prefix">
                                            <i class="layui-icon layui-icon-cellphone"></i>
                                        </div>
                                        <input type="text" name="phone" value="" lay-verify="required|phone"
                                               placeholder="手机号" lay-reqtext="请填写手机号" autocomplete="off"
                                               class="layui-input" id="regPhone">
                                    </div>
                                </div>
                                <div class="layui-col-xs5">
                                    <div style="margin-left: 11px;">
                                        <button type="button" class="layui-btn layui-btn-fluid layui-btn-primary"
                                                id="sendMsg">获取验证码
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-vercode"></i>
                                </div>
                                <input type="text" name="code" value="" lay-verify="required" placeholder="验证码"
                                       lay-reqtext="请填写验证码" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-username"></i>
                                </div>
                                <input type="text" name="username" value="" lay-verify="required|uname"
                                       placeholder="用户名" lay-reqtext="请填写用户名"
                                       autocomplete="off" class="layui-input" lay-affix="clear">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-password"></i>
                                </div>
                                <input type="password" name="password" value="" lay-verify="required" placeholder="密码"
                                       autocomplete="off" class="layui-input" id="reg-password" lay-affix="eye">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-password"></i>
                                </div>
                                <input type="password" name="confirmPassword" value=""
                                       lay-verify="required|confirmPassword" placeholder="确认密码" autocomplete="off"
                                       class="layui-input" lay-affix="eye">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="regBtn">注册</button>
                        </div>
                        <div class="layui-form-item" style="text-align: right">
                            已有帐号直接 <a href="javascript:;" style="color: #01aaed" onclick="changeTab(0)">登录</a>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
</body>
<script src="static/common/js/ajax.js"></script>
<script src="static/common/js/base.js"></script>
<script>
    let element;
    let uuid;//图片验证码uuid
    let codeUUID;//短信验证码uuid
    layui.use(['form', 'element'], function () {
        const form = layui.form;
        element = layui.element;
        changeTab('${type}');
        getKaptcha();
        // 自定义验证规则
        form.verify({
            // 确认密码
            confirmPassword: function (value) {
                var passwordValue = $('#reg-password').val();
                if (value !== passwordValue) {
                    return '两次密码输入不一致';
                }
            },
            uname: function (value) {
                if (!(/^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/.test(value))) {
                    return "由6-20长度的字母数字下划线组成";
                }
            }
        });
        /*监听注册按钮*/
        form.on('submit(regBtn)', function (data) {
            data.field.uuid = codeUUID;
            http.post('blog/register', data.field, function () {
                layer.alert('注册成功，使用用户名密码进行登录！', {icon: 6}, function (index) {
                    document.getElementById("registerForm").reset();
                    changeTab(0);
                    layer.close(index);
                });
            });
            return false;
        });
        form.on('submit(loginBtn)', function (data) {
            data.field.uuid = uuid
            http.post('blog/login', data.field, function (res) {
                if (data.field.remember) {
                    const date = new Date();
                    date.setDate(date.getDate() + 7);
                    date.setHours(date.getHours() + 8);
                    Cookies.set('token', res.msg, {
                        expires: date,
                        path: "/"
                    })
                } else {
                    Cookies.set('token', res.msg, {path: "/"})
                }

                layer.msg('登录成功！', {icon: 6}, function () {
                    top.location.reload()
                });
            }, function (res) {
                getKaptcha();
                $("#password").val("");
                $("#code").val("");
                layer.alert(res.msg, {icon: 5});
            })
            return false;
        });

        /*发送短信按钮*/
        $("#sendMsg").click(function () {
            const phone = $("#regPhone").val();
            if (!phone) {
                layer.msg('请输入手机号码!', {icon: 2});
                return false;
            }
            if (!(/^1\d{10}$/.test(phone))) {
                layer.msg('手机号码格式错误!', {icon: 2});
                return false;
            }
            $(this).addClass('layui-btn-disabled');
            let s = 59;
            $(this).text(s + "秒后重试...");
            let inter = setInterval(() => {
                s--;
                if (s === 0) {
                    $(this).removeClass("layui-btn-disabled").text("获取验证码")
                    clearInterval(inter);
                } else {
                    $(this).text((s < 10 ? '0' + s : s) + "秒后重试...");
                }

            }, 1000);
            http.get('blog/send', {phone}, function (res) {
                layer.msg('发送成功！', {icon: 6});
                codeUUID = res.msg
            })
        });
    })

    function changeTab(id) {
        element.tabChange('loginTab', id)
    }


    function generateUUID() {
        let d = new Date().getTime();
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
            let r = (d + Math.random() * 16) % 16 | 0;
            d = Math.floor(d / 16);
            return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
        });
    }

    function getKaptcha() {
        uuid = generateUUID();
        const url = 'kaptcha.jpg?uuid=' + uuid;
        $("#kaptchaImage").prop('src', url);
    }

    function forget() {
        /*获取当前窗口的标记值*/
        const index = parent.layer.getFrameIndex(window.name);
        /*关闭窗口*/
        parent.layer.close(index);
        parent.layer.open({
            type: 2,
            content: "page/forget",
            area: ["400px", "500px"],
            title: "忘记密码"
        })
    }
</script>
</html>
